package com.fs.files.service.impl;

import com.fs.files.service.CimeService;
import com.fs.files.service.FileService;
import com.fs.files.util.CimeUtil;
import com.fs.files.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Service
public class CimeServiceImpl implements CimeService {

    private static final Logger logger = LoggerFactory.getLogger(CimeServiceImpl.class);

    @Autowired
    private FileService fileService;

    @Override
    public LinkedHashMap<String, List<Object>> readCim(MultipartFile multipartFile, String destDirPath, String fileName) throws Exception {
        LinkedHashMap<String, List<Object>> linkedHashMap = new LinkedHashMap<>();
        String fileType = FileUtils.getExtend(multipartFile.getOriginalFilename());
        fileName = fileName+"."+ fileType;
        String filePath = System.getProperty("user.dir") + File.separator + destDirPath;
        String fullPath = filePath+ File.separator + fileName;
        InputStream input = multipartFile.getInputStream();// 输入流
        fileService.savePic(input, fileName, filePath);
        File file = new File(fullPath);
        InputStreamReader read = null;
        if (file.isFile() && file.exists()) {
            logger.info("读取cim文件开始："+fullPath);
            read = new InputStreamReader(new FileInputStream(file), "utf-8");
            BufferedReader bufferedReader = new BufferedReader(read);
            //行数据
            String lineTxt;
            //cime文件中的类名称
            String cimClassName = "";
            //类实例
            Class cls = null;
            //类属性
            String lineProperties = "";
            //类数据集合
            List<String> valueList = new ArrayList<>();
            //类数据集合结果
            List<Object> objList = new ArrayList<>();
            while ((lineTxt = bufferedReader.readLine()) != null) {
                if(lineTxt.startsWith("<cim")) {
                    //解析cime文件中的类名称
                    cimClassName = CimeUtil.getCimClassName(lineTxt);
                    //根据名称获取类实例
                    cls = CimeUtil.getInstance("com.jb.mpc.rps.starter.dto", cimClassName);
                }
                else if (lineTxt.startsWith("@")){
                    //解析CIME中的类属性 @Num mRID等
                    lineProperties = lineTxt;
                }
                else if (lineTxt.startsWith("#")) {
                    valueList.add(lineTxt);
                }
                else if (lineTxt.startsWith("</")) {
                    //类数据读取结束
                    objList = CimeUtil.getObjectList(cls, lineProperties, valueList);
                    linkedHashMap.put(cimClassName, objList);
                    valueList.clear();
                }
            }
            logger.info("读取cim文件结束："+filePath);
        }
        return linkedHashMap;
    }
}
